<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Resources\Result;
use App\Models\Record;
use App\Models\Ranking;
use App\Models\Score;
use App\Models\User;

class GameController extends Controller
{
    public function userRecord($id)
    {
        $records = Record::select('sequence', 'create_time')
        ->where('user_id', $id)
        ->orderBy('create_time', 'desc')
        ->get();
        return Result::OK('查询成功', $records);
    }

    public function ranking(Request $request)
    {
        $request->validate([
            'page' => 'required|integer',
            'pageSize' => 'integer|min:1'
        ]);

        $pageSize = $request->input('pageSize', 5);


        $ranking = Ranking::select('*')
        ->RankingBase()
        ->simplePaginate($pageSize);
        return Result::OK('查询成功', $ranking);
    }

    public function solve(Request $request)
    {
        $request->validate([
            'id' => 'required|int',
            'solve' => 'required|array',
            'time' => 'required|numeric',
            'withAssist' => 'required|boolean'
        ]);

        $id = $request->input('id');
        $solve = $request->input('solve');
        $time = $request->input('time');
        $withAssist = $request->input('withAssist');
        if(!$this->checkSolve($solve)) {
            return Result::FAIL(400, '解谜失败，请加油！');
        }

        $sequence = implode('', $solve);
        // with(['score', 'record', 'ranking'])->
        $user = User::find($id);
        $score = $user->score;
        $record = $user->record();

        if($withAssist) {
            $score->fastest_time_with_assist = $this->myMin($score->fastest_time_with_assist, $time);
        } else {
            $score->fastest_time_without_assist = $this->myMin($score->fastest_time_without_assist, $time);
        }

        $exist = $record->where('sequence', $sequence)->exists();
        // 新解法
        if(!$exist) {
            $ranking = $user->ranking;
            // null+1 == 1
            $ranking->solve_num += 1;
            $ranking->user_name = $user->name;
            // 要先save才能获取排名
            $ranking->save();

            $userRank = Ranking::getUserRank($id);
            $score->best_rank = $this->myMin($score->best_rank, $userRank);
            $score->solve_num += 1;
            $record->create([
                'sequence' => $sequence
            ]);

        }

        $score->save();

        return Result::OK('解谜成功！');
    }

    /**
     * 八皇后算法
     */
    public function checkSolve($solve)
    {
        if(count($solve)!=8) {
            return false;
        }

        $column = array_fill(0, 8, false); // |
        $slash = array_fill(0, 15, false); // /
        $backslash = array_fill(0, 15, false); // \

        for ($i=0; $i < 8; $i++) {
            $j = $solve[$i];
            if($column[$j]  || $slash[$j-$i+7] || $backslash[$j+$i]) {
                return false;
            }
            $column[$j] = $slash[$j-$i+7] = $backslash[$j+$i] = true;
        }
        return true;
    }

    /**
     * 考虑null的情况
     */
    public function myMin($value1, $value2)
    {
        return $value1==null ? $value2 : min($value1, $value2);
    }
}
